package dxunderground.winampController.Server.GUI;

import java.awt.BorderLayout;
import java.io.IOException;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import dxunderground.winampController.Common.GUI;

@SuppressWarnings("serial")
public class serverGUI extends GUI {
	public serverGUI(boolean recentJava) throws JDOMException, IOException {
		if (recentJava) {
			SAXBuilder builder = new SAXBuilder();
			Document doc = builder.build("/Program Files/Winamp Controller/Server Preferences.xml");
			
			Element properties = doc.getRootElement();
			ButtonActions buttonActions = new ButtonActions(this, properties);
			JPanel controlPanel = new ControlPanel(buttonActions, this);
			ServerPanel serverPanel = new ServerPanel(buttonActions, this);

			WindowActions windowActions = new WindowActions(serverPanel, this, properties);
			this.addWindowListener(windowActions);

			this.setLayout(new BorderLayout());
			this.add(controlPanel, BorderLayout.CENTER);
			this.add(serverPanel, BorderLayout.EAST);
		} else {
			JPanel panel = new JPanel(new BorderLayout());
			panel.add(new JLabel("Update your java."), BorderLayout.NORTH);
			JTextField field = new JTextField("http://www.java.com/en/download/windows_xpi.jsp?locale=en&host=www.java.com:80");
			field.setEditable(false);
			panel.add(field, BorderLayout.SOUTH);
			this.add(panel);
			this.pack();
		}
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setVisible(true);
		this.setResizable(true);
	}

	public static void main(String[] args) throws JDOMException, IOException {
		boolean recentJava = true;
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (UnsupportedLookAndFeelException e) {
			recentJava = false;
		} catch (InstantiationException e) {
			recentJava = false;
		} catch (IllegalAccessException e) {
			recentJava = false;
		} catch (ClassCastException e) {
			recentJava = false;
		} catch (ClassNotFoundException e) {
			recentJava = false;
		}
		new serverGUI(recentJava);
	}
}